package com.nd.android.musicboxlibrary.util;

import com.nd.android.musicboxlibrary.business.comm.MagicNumber;

import java.util.Calendar;

/**
 * 提供关于时间格式化的函数
 * 
 * @author mk
 */
public final class TimeUtil implements MagicNumber {
    
    /**
     * 
     * <br>Created 2014年11月28日 上午12:05:46
     * @author       wat.ztag
    */
    @Override
    public void initMagicNumber() {
    }

    /**
     * Creates a new instance of TimeUtil. <br>
     * Created 2014年9月11日 下午11:15:44
     */
    private TimeUtil() {
    }

    /**
     * 把ms转化成hh:mm:ss的时间格式， 若秒合算之后达到一个小时，则以"HH:mm:ss"格式提供
     * @param timestamp
     * 时间戳
     * @return String
     */
    public static String formatSecondsToCommaStyle(long timestamp) {
        final long oneHourToMs = 3600000;
        String pattern = "";
        if (oneHourToMs > timestamp) {
            pattern = "mm‘ss";
        } else {
            pattern = "hh‘mm‘ss";
        }
        FastDateFormat df = FastDateFormat.getInstance(pattern, null, null);
        return df.format(timestamp);
    }

    /**
     * 将unix时间戳转换成yyy-MM-dd HH:mm 格式
     * 
     * @param timestamp
     *            单位s
     * @return String
     */
    public static String format2EventTime(long timestamp) {
        long time = timestamp * n1000;
        String pattern = "yyyy-MM-dd HH:mm";
        FastDateFormat df = FastDateFormat.getInstance(pattern, null, null);
        return df.format(time);
    }

    /**
     * 格式化时间，简化成xx秒，xx分钟，xx天前
     * 
     * @param timestamp
     *            unix时间戳 单位s
     * @return 格式化后的字符串
     */
    public static String format2HumanTime(long timestamp) {
        timestamp *= n1000L;
        long diffSecond = System.currentTimeMillis() - timestamp;
        diffSecond = diffSecond > 0 ? diffSecond : 0;

        final long MINUTE = 60 * 1000L;
        final long HOUR = MINUTE * 60;

        if (diffSecond < HOUR) {
            long minute = diffSecond / MINUTE;
            minute = minute == 0 ? 1 : minute;
            return minute + "分钟前";
        }

        StringBuilder formateDate = new StringBuilder();
        String pattern = null;
        Calendar now = Calendar.getInstance();
        Calendar base = Calendar.getInstance();
        base.setTimeInMillis(timestamp);

        do {
            int diffYear = now.get(Calendar.YEAR) - base.get(Calendar.YEAR);
            if (diffYear > 0) {
                pattern = "yyyy-MM-dd HH:mm";
                break;
            }

            int diffDate = now.get(Calendar.DAY_OF_YEAR) - base.get(Calendar.DAY_OF_YEAR);
            if (diffDate == 0) {
                formateDate.append("今天 ");
                pattern = "HH:mm";
            } else if (diffDate == 1) {
                formateDate.append("昨天 ");
                pattern = "HH:mm";
            } else {
                pattern = "MM-dd HH:mm";
            }
        } while (false);

        FastDateFormat df = FastDateFormat.getInstance(pattern, null, null);
        formateDate.append(df.format(timestamp));
        return formateDate.toString();
    }

    /**
     * 
     * @return long
     */
    public static long getCurrentTime() {
        return System.currentTimeMillis() / n1000;
    }

}
